package org.jwap.server.jwapPage;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.html.parser.Parser;

import org.jwap.server.JWAP;
import org.jwap.server.helper.SerialCommunication;
import org.jwap.server.utils.FileCore;
import org.jwap.server.utils.JwapPage;
import org.jwap.server.utils.Key;


public class SmartHouse extends JwapPage{
	public static Lamp loblect = new Lamp();
	public String Response(HttpData data){
		for(Key<String> k : data.getData.getList()){
			String[] key = k.getKey().split("[");
			String lampKey = key[2].substring(0,1);
			int lampNo = Integer.parseInt(lampKey);
			if(k.getValue().equals("true")){
				Lamp.processMessage(lampNo, true);
			}else{
				Lamp.processMessage(lampNo, false);
			}
			
		}
		return "";
	}
	
	public static class Lamp{
		boolean lampStatus = false;
		String lampName;
		int lampNo;
		
		List<Lamp> lampList = new ArrayList<>();
		
		public void init(){
			for(int a = 0; a<16; a++){
				Lamp lamp = new Lamp();
				lamp.lampNo = a;
				lampList.add(lamp);
			}
		}
		public static void processMessage(int lampNo, boolean status){
			  try
		        {
		        	SerialCommunication com = new SerialCommunication();
		            (com).connect(JWAP.serialPort);
		            com.sendSerialMessage(processString(lampNo, status));
		           
		        }
		        catch ( Exception e )
		        {
		            // TODO Auto-generated catch block
		            e.printStackTrace();
		        }
		}
		
		public static String processString(int lampNo, boolean status){
			String lampKey = Integer.toString(lampNo);
			String actionKey = "K";
			if(lampKey.length()<2){
				lampKey = "0"+lampKey;
			}
			if(status){
				actionKey = "A";
			}
			
			return "~L"+actionKey+lampNo+"000|";
			
		}
	}

}
